package com.hui.account.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TimeUtils {

    public static final String TIME_SQL_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String TIME_SQL_PATTERN_DATA = "yyyy年MM月dd日";

    public static final String TIME_SQL_PATTERN_TIME = "yyyy年MM月dd日 HH:mm";

    /**
     * 获取系统当前时间
     */
    public static Timestamp getCurrentTime() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 获取系统当前日期
     */
    public static String getTodayDate() {
        java.util.Date date = new java.util.Date();
        SimpleDateFormat format = new SimpleDateFormat(TIME_SQL_PATTERN_DATA);
        return format.format(date);
    }

    /**
     * 获取系统当前日期
     */
    public static String getTodayTime() {
        java.util.Date date = new java.util.Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    /**
     * 将字符串转换为时间类
     */
    public static Timestamp getTimestampToString(String time) throws ParseException {
        if (time == null || time.isEmpty() || time.equals("null"))
            return null;

        return new Timestamp(new SimpleDateFormat(TIME_SQL_PATTERN).parse(time).getTime());
    }

    /**
     * 将字符串转换为时间类
     */
    public static Timestamp getTimestampToString(String time, String pattern) throws ParseException {
        if (time == null || time.isEmpty() || time.equals("null"))
            return null;

        return new Timestamp(new SimpleDateFormat(pattern).parse(time).getTime());
    }

    /**
     * 将时间类转换为字符串
     */
    public static String getStringToTimestamp(Timestamp time, String pattern) {
        if (time == null)
            return null;

        return new SimpleDateFormat(pattern).format(time.getTime());
    }

    /**
     * 将时间毫秒值转字符串时间
     */
    public static String longTransferTime(long lastModified) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(lastModified);
    }
}
